package com.example.yjh_login.presenterImp;

import com.example.lib_mvp.base.AppBasePresenter;
import com.example.lib_mvp.base.mvp.BaseView;
import com.example.lib_mvp.httpUtil.HdBaseBean;
import com.example.lib_mvp.httpUtil.HttpUrl;
import com.example.lib_mvp.httpUtil.HttpUtil;
import com.example.yjh_login.beans.HdLoginBean;
import com.google.gson.reflect.TypeToken;
import com.tjf.lib_utils.EventBusUtils;
import com.tjf.lib_utils.GsonUtils;

import java.lang.reflect.Type;

public class LoginPresenter extends AppBasePresenter<BaseView> {
    public void getHdRegister(String phone, String code, String password, String pwd) {
        HttpUtil.getHdRegister(phone, code, password, pwd, this);
    }

    public void getHdLogin(int type, String phone, String code, String pwd) {
        HttpUtil.getHdLogin(phone, code, pwd, type, this);
    }

    public void getHdOneLogin(String loginToken) {
        HttpUtil.getHdOneLogin(loginToken, this);
    }

    public void getHdLoginSms(String phone) {
        HttpUtil.getHdLoginSms(phone, this);
    }

    public void getHdMyActivation(String orderId) {
        HttpUtil.getHdMyActivation(orderId, this);
    }


    @Override
    public void onDataBack(String url, int code, String data) {
        getAipEventData(url, HttpUrl.LOGIN_REGISTER, code, data);
        getAipEventData(url, HttpUrl.LOGIN_ONE_LOGIN, code, data);
        getAipEventData(url, HttpUrl.LOGIN_LOGIN, code, data);
        getAipEventData(url, HttpUrl.LOGIN_SMS, code, data);
        getAipEventData(url, HttpUrl.MY_ACTIVATION, code, data);
    }

    public void getAipEventData(String url, String tagUrl, int code, String data) {
        if (url.contains(tagUrl)) {
            if (code == 0) {
                if (url.contains(HttpUrl.LOGIN_LOGIN) || url.contains(HttpUrl.LOGIN_ONE_LOGIN)) {
                    Type type = new TypeToken<HdBaseBean<HdLoginBean>>() {
                    }.getType();
                    GsonUtils.getType(HdBaseBean.class, HdLoginBean.class);
                    HdBaseBean<HdLoginBean> baseBean = GsonUtils.fromJson(data, type);
                    EventBusUtils.sendEvent(tagUrl, baseBean.getData());
                    return;
                }
                if (url.contains(HttpUrl.LOGIN_SMS)) {
                    EventBusUtils.sendEvent(tagUrl, data);
                    return;
                }
                EventBusUtils.sendEvent(tagUrl, data);
            } else {
                EventBusUtils.sendEvent(tagUrl, code, data);
            }
        }
    }

}
